import Vue from 'vue'

import VueRouter from 'vue-router'

// 导入进度条插件
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)

import login from '@/views/login/login.vue'
import home from '@/views/home/layout.vue'

import chart from '@/views/home/chart/chart.vue'
import subject from '@/views/home/subject/subject.vue'
import business from '@/views/home/business/business.vue'
import question from '@/views/home/question/question.vue'
import userList from '@/views/home/userList/userList.vue'

const router = new VueRouter({
    routes: [
        {
            path: '/',
            component: login,
            meta: {
                title: '登录',
                rules: ['超级管理员', '管理员', '老师', '学生']
            }
        },
        {
            path: '/login',
            component: login,
            meta: {
                title: '登录',
                rules: ['超级管理员', '管理员', '老师', '学生']
            }
        },
        {
            path: '/home',
            component: home,
            redirect: '/home/subject',
            children: [
                {
                    path: 'chart',
                    component: chart,
                    // 路由元信息
                    meta: {
                        title: '数据概览',
                        icon: 'el-icon-time',
                        rules: ['超级管理员', '管理员', '老师']
                    }
                },
                {
                    path: 'subject',
                    component: subject,
                    meta: {
                        title: '学科列表',
                        icon: 'el-icon-notebook-2',
                        rules: ['超级管理员', '管理员', '老师', '学生']
                    }
                },
                {
                    path: 'business',
                    component: business,
                    meta: {
                        title: '企业列表',
                        icon: 'el-icon-office-building',
                        rules: ['超级管理员', '管理员', '老师']
                    }
                },
                {
                    path: 'question',
                    component: question,
                    meta: {
                        title: '题库列表',
                        icon: 'el-icon-edit-outline',
                        rules: ['超级管理员', '管理员', '老师']
                    }
                },
                {
                    path: 'userList',
                    component: userList,
                    meta: {
                        title: '用户列表',
                        icon: 'el-icon-user',
                        rules: ['超级管理员', '管理员']
                    }
                }
            ]
        },
        {
            path: '*',
            component: login,
            meta: {
                title: '登录'
            }
        }
    ]
})

import { Message } from 'element-ui'
import store from '@/store/index.js'
import { removeToken } from '@/utils/token.js'

// 重复点击路由重复解决方案：
const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

// 设置导航守卫
router.beforeEach((to, from, next) => {
    // 路由进入前的处理
    // 开启进度条
    Nprogress.start()

    // 因为生命周期只执行一次，所以还需在导航守卫中再判断
    if(to.meta.rules.includes(store.state.role)){
        next()
    }else{
        Message.warning('您没有权限访问此页面')
        // 删除token
        removeToken()
        // 跳转至登录页
        next('/')
    }
})

router.afterEach((to) => {
    // 路由进入后的处理
    Nprogress.done()
    
    // 修改网页标题
    document.title = to.meta.title
})

export default router